package com.spider.admin.store.primary.mapper;

import com.spider.admin.store.primary.model.AgentOption;
import com.spider.admin.store.primary.model.AgentSettingInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface AgentSettingMapper {

    @Select({"<script>",
            "SELECT count(0) ",
            "FROM t_agent_options tao LEFT JOIN t_agent_setting tas ON tao.section=tas.section AND tao.option_item=tas.option_item ",
            " WHERE tao.section=#{section} ",
            "<if test='optionItem != null'> AND tao.option_item like #{optionItem}</if>",
            "<if test='belongType != null'> AND (tas.belong_type=#{belongType} OR tas.belong_type IS NULL)</if>",
            "<if test='belongId != null'> AND (tas.belong_id=#{belongId} OR tas.belong_id IS NULL)</if>",
            "</script>"})
    Long queryAgentSettingNum(String section, String optionItem, String belongType, String belongId);

    @Select({"<script>",
            "SELECT tas.pid,tas.belong_id,tas.belong_type,tas.option_value,tas.setting_desc,tao.option_desc,tao.section,tao.option_item ",
            "FROM t_agent_options tao LEFT JOIN t_agent_setting tas ON tao.section=tas.section AND tao.option_item=tas.option_item ",
            " WHERE tao.section=#{section} ",
            "<if test='optionItem != null'> AND tao.option_item like #{optionItem}</if>",
            "<if test='belongType != null'> AND (tas.belong_type=#{belongType} OR tas.belong_type IS NULL)</if>",
            "<if test='belongId != null'> AND (tas.belong_id=#{belongId} OR tas.belong_id IS NULL)</if>",
            " order by tao.option_item",
            " limit #{pageSize} offset #{startRow}",
            "</script>"})
    @Results(value = {
            @Result(column = "pid", property = "pid"),
            @Result(column = "belong_id", property = "belongId"),
            @Result(column = "belong_type", property = "belongType"),
            @Result(column = "option_value", property = "optionValue"),
            @Result(column = "setting_desc", property = "settingDesc"),
            @Result(column = "option_desc", property = "optionDesc"),
            @Result(column = "option_item", property = "optionItem"),
            @Result(column = "section", property = "section")
    })
    List<AgentSettingInfo> queryAgentSettings(int startRow, Integer pageSize, String section, String optionItem, String belongType, String belongId);

    @Insert({"insert into t_agent_setting(pid,section,option_item,option_value,setting_desc,belong_type,belong_id) values(#{pid},#{section},#{optionItem},#{optionValue},#{settingDesc},#{belongType},#{belongId})"})
    int insertSettingInfo(AgentSettingInfo agentSettingInfo);

    @Update({"update t_agent_setting set option_value=#{optionValue},setting_desc=#{settingDesc} where pid=#{pid}"})
    int updateSettingInfo(AgentSettingInfo agentSettingInfo);

    @Select({"<script>",
            "SELECT tenant_id pid,tenant_id nodeId,tenant_name nodeName,'tenant' nodeType,'root' parentId FROM t_account_info WHERE archive_status='active' ",
            "<if test='tenantId != null'> AND tenant_id = #{tenantId}</if>",
            " UNION ALL ",
            " SELECT pid,pid nodeId,cloud_name nodeName,'cloud' nodeType,tenant_id parentId FROM t_cloud_info WHERE archive_status='active' ",
            "<if test='tenantId != null'> AND tenant_id = #{tenantId}</if>",
            " UNION ALL ",
            " SELECT pid,pid nodeId,app_name nodeName,'server' nodeType,cloud_id parentId FROM t_application_info WHERE app_status='active' ",
            "<if test='tenantId != null'> AND tenant_id = #{tenantId}</if>",
            "</script>"})
    List<Map<String, String>> queryAgentServerList(String tenantId);
    @Select({"<script>",
            "SELECT tenant_id pid,tenant_id nodeId,tenant_name nodeName,'tenant' nodeType,'root' parentId FROM t_account_info WHERE archive_status='active' ",
            "<if test='tenantId != null'> AND tenant_id = #{tenantId}</if>",
            " UNION ALL ",
            " SELECT pid,pid nodeId,cloud_name nodeName,'cloud' nodeType,tenant_id parentId FROM t_cloud_info WHERE archive_status='active' ",
            "<if test='tenantId != null'> AND tenant_id = #{tenantId}</if>",
            "</script>"})
    List<Map<String, String>> queryAgentServerList2(String tenantId);
    /**
     * 代理配置级联删除
     *
     * @param pid
     * @return
     */
    @Delete({"DELETE tas FROM t_agent_setting tas, t_agent_options tao WHERE tao.section=tas.section AND tao.option_item=tas.option_item AND tao.pid=#{pid}"})
    int deleteWithOptionPid(String pid);

    @Select({
            "SELECT * ",
            "FROM t_agent_setting ",
            "  WHERE (section='global' OR section='events' OR section='http') AND option_value IS NOT NULL AND option_value!=''"})
    @Results(value = {
            @Result(column = "pid", property = "pid"),
            @Result(column = "belong_id", property = "belongId"),
            @Result(column = "belong_type", property = "belongType"),
            @Result(column = "option_value", property = "optionValue"),
            @Result(column = "setting_desc", property = "settingDesc"),
            @Result(column = "option_desc", property = "optionDesc"),
            @Result(column = "option_item", property = "optionItem"),
            @Result(column = "section", property = "section")
    })
    List<AgentSettingInfo> queryGlobalAgentConfList();

    @Select({
            "SELECT * ",
            "FROM t_agent_setting ",
            "  WHERE section='server'  AND belong_id=#{serverId}"})
    @Results(value = {
            @Result(column = "pid", property = "pid"),
            @Result(column = "belong_id", property = "belongId"),
            @Result(column = "belong_type", property = "belongType"),
            @Result(column = "option_value", property = "optionValue"),
            @Result(column = "setting_desc", property = "settingDesc"),
            @Result(column = "option_desc", property = "optionDesc"),
            @Result(column = "option_item", property = "optionItem"),
            @Result(column = "section", property = "section")
    })
    List<AgentSettingInfo> queryServerAgentConfList(String serverId);

    @Select({
            "SELECT * ",
            "FROM t_agent_setting ",
            "  WHERE section='location'  AND belong_id=#{appId}"})
    @Results(value = {
            @Result(column = "pid", property = "pid"),
            @Result(column = "belong_id", property = "belongId"),
            @Result(column = "belong_type", property = "belongType"),
            @Result(column = "option_value", property = "optionValue"),
            @Result(column = "setting_desc", property = "settingDesc"),
            @Result(column = "option_desc", property = "optionDesc"),
            @Result(column = "option_item", property = "optionItem"),
            @Result(column = "section", property = "section")
    })
    List<AgentSettingInfo> queryAppAgentConfList(String appId);

}
